import type { RouteRecordRaw } from 'vue-router'

import router from '@/router'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
import { useUserStoreWithOut } from '@fl/store/modules/user'
import { listToTree } from '@/store/modules/helper'
import { iamMenulist, testRouter, userRouters } from './test'
// 根据角色设置路由
export async function setRoutesByRole(data) {
    const userStore = useUserStoreWithOut()
    const permissionStore = usePermissionStoreWithOut()
    let roleRouters: any[] = []
    const { userFlag, userStep } = data as any
    // 企业管理员进来就先创建企业，用的是前段写的路由
    if (userFlag === '2' && ['1', '2'].includes(userStep)) {
        roleRouters = listToTree(iamMenulist)
    }
    else {
        roleRouters = listToTree(data.iamAppMenuVoList)
        // console.log('运营端获取菜单getUserMenu', roleRouters)
    }
    // console.log('🚀 ~ ···· roleRouters:', roleRouters)
    window.sessionStorage.setItem('USER_MENU', JSON.stringify(roleRouters))

    userStore.setRoleRouters(roleRouters)
    const path = roleRouters[0]?.path
    // 提前把分片路由切一刀, 免得侧边栏空白
    permissionStore.setSliceRouters(path)
    router.push('/')
    // router.push({ path: permissionStore.addRouters[0].path })
}
